package com.yhy.subject.application.controller;

import com.google.common.base.Preconditions;
import com.mysql.cj.util.StringUtils;
import com.yhy.basic.entity.SubjectCategory;
import com.yhy.endity.Result;
import com.yhy.entity.SubjectCategoryBO;
import com.yhy.service.SubjectCategoryDomainService;
import com.yhy.subject.application.convert.SubjectCategoryDTOConvert;
import com.yhy.subject.application.dto.SubjectCategoryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/subject/category")
public class SubjectCategoryController {

    @Autowired
    private SubjectCategoryDomainService subjectCategoryDomaineService;

    @PostMapping("/add")
    public Result<Boolean> test(@RequestBody SubjectCategoryDTO subjectCategoryDto) {
        Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(), "分类类型不能为空");
        Preconditions.checkNotNull(subjectCategoryDto.getParentId(), "分类父级不能为空");
        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertBoToCategory(subjectCategoryDto);
        subjectCategoryDomaineService.add(subjectCategoryBO);
        return Result.success(true);
    }

    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory() {
        try {
            SubjectCategoryBO subjectCategoryBO = new SubjectCategoryBO();
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomaineService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConvert.INSTANCE.convertBoToCategoryDTO(subjectCategoryBOList);
            return Result.success(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("查询一级分类失败", e);
            return Result.error("查询一级分类失败");
        }


    }

    @PostMapping("/queryCategory")
    public Result<List<SubjectCategoryDTO>> queryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {

        try {
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "父类id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertBoToCategoryBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomaineService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConvert.INSTANCE.convertBoToCategoryDTO(subjectCategoryBOList);
            return Result.success(subjectCategoryDTOList);
        } catch (Exception e) {
            log.error("查询一级分类失败", e);
            return Result.error("查询一级分类失败");
        }


    }

    //更新分类123
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConvert.INSTANCE.convertBoToCategoryBO(subjectCategoryDTO);
            Boolean b = subjectCategoryDomaineService.update(subjectCategoryBO);
            return Result.success(b);
        } catch (Exception e) {
            log.error("更新失败", e);
            return Result.error("更新失败失败");
        }


    }






}
